package com.mxprojects.gui
{
	import com.mxprojects.constructors.Divider;
	import com.mxprojects.constructors.DrawBorder;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.display.Sprite;

	public class BaseForumElement extends MovieClip
	{
		public var background:MovieClip;
		public var box:Sprite;
		
		private var hasBorder:Boolean = false;
		private var borderColour:uint;
		private var isBackgroundStretched:Boolean;
		
		public function BaseForumElement(){	}
		
		public function init(cssStyle:String, dividerCount:int = 0, stretchBackground:Boolean = true):void {
			//setup the basic elements
			if(stretchBackground){
				background.width = MXP3.wStage;
			}
			
			isBackgroundStretched = stretchBackground;
			
			//check if they want a border
			if(Styles.css.getStyle(cssStyle)["borderColor"] != undefined){
				hasBorder = true;
				borderColour = Styles.colorToUint(cssStyle, "borderColor");
				box = DrawBorder.create(background, borderColour);
				addChild(box);
			}
			
			//see if there are any dividers
			if(dividerCount > 0){
				addDividers(dividerCount);
			}
			
			//set the CSS
			Styles.setProperties(background, cssStyle);
		}
		
		public function resize():void {
			//resizes elements with the most basic of things. Need more? Do an override!
			if(isBackgroundStretched){
				background.width = MXP3.wStage;
			}
			
			if(hasBorder){
				DrawBorder.redraw(background, this);
			}
			
			//see if there was a bg image
			var bgImage:DisplayObject = getChildByName("backgroundImage");
			if(bgImage != null){
				bgImage.width = MXP3.wStage;
			}	
		}
		
		public function getDivider(dividerNumber:int):Divider {
			return getChildByName("divider" + dividerNumber) as Divider;
		}
		
		private function addDividers(dividerCount:int):void {
			//this will add dividers to the element and give them names divider0, divider1, divider2, etc.
			for(var i:int = 0; i < dividerCount; i++){
				var divider:Divider = new Divider(height, borderColour);
				divider.name = "divider" + i;
				addChild(divider);
			}
		}
	}
}